package edu.xhu.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper{

	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		createTable(db);
	}
	
	
	/*public void createtable_f(SQLiteDatabase db,String tableName){
		
	}*/
	
	public void createTable(SQLiteDatabase db){
		//创建课程名单表
		String sql = "CREATE TABLE IF NOT EXISTS " + DatabaseAdapter.COURSE_TABLE + "("
				+ DatabaseAdapter.ID + " INTEGER PRIMARY KEY,"
				+ DatabaseAdapter.COURSE_NAME + " STRING);";
		db.execSQL(sql);
		
		//创建学生名单表
		sql = "CREATE TABLE IF NOT EXISTS " + DatabaseAdapter.STUDENT_TABLE + "("
				+ DatabaseAdapter.ID + " INTEGER PRIMARY KEY,"
				+ DatabaseAdapter.STUDENT_NUMBER + " STRING,"
				+ DatabaseAdapter.STUDENT_NAME + " STRING,"
				+ DatabaseAdapter.STUDENT_MAJOR + " STRING,"
				+ DatabaseAdapter.STUDENT_CLASSES + " STRING,"
				+ DatabaseAdapter.STUDENT_GRADES + " STRING,"
				+ DatabaseAdapter.STUDENT_CREDIT + " STRING,"
				+ DatabaseAdapter.STUDENT_REBUILD + " STRING,"
				+ DatabaseAdapter.STUDENT_COURSE + " STRING,"
				+ DatabaseAdapter.STUDENT_REMARK + " STRING);";
		db.execSQL(sql);
		
		//创建用户表
		sql = "CREATE TABLE IF NOT EXISTS " + DatabaseAdapter.USER_TABLE + "("
				+ DatabaseAdapter.USER_ID + " STRING,"
				+ DatabaseAdapter.USER_PWD + " STRING,"
				+ DatabaseAdapter.USER_ROLE + " STRING,"
				+ DatabaseAdapter.USER_NAME + " STRING);";
		db.execSQL(sql);
		
		//创建软件主题信息表
		sql = "CREATE TABLE IF NOT EXISTS " + DatabaseAdapter.SOFTWARE_THEME + "("
				+ DatabaseAdapter.THEME + " STRING);";
		db.execSQL(sql);
		
		//创建图书馆账户用户信息表
		sql = "CREATE TABLE IF NOT EXISTS " + DatabaseAdapter.LIBRARY_TABLE + "("
				+ DatabaseAdapter.USER_ID + " STRING,"
				+ DatabaseAdapter.USER_PWD + " STRING);";
		db.execSQL(sql);
		
		//创建教师课程信息表
		sql = "CREATE TABLE IF NOT EXISTS " + DatabaseAdapter.TEACHER_TIME_TABLE + "("
				+ DatabaseAdapter.ID + " STRING,"
				+ DatabaseAdapter.COURSE_NAME1 + " STRING,"
				+ DatabaseAdapter.TEACHER + " STRING,"
				+ DatabaseAdapter.PLACE	+ " STRING,"
				+ DatabaseAdapter.TIME	+ " STRING,"
				+ DatabaseAdapter.MORE	+ " STRING,"
				+ DatabaseAdapter.REMARK + " STRING);";
		db.execSQL(sql);
		
		//创建学生课程信息表
		sql = "CREATE TABLE IF NOT EXISTS " + DatabaseAdapter.STUDENT_TIME_TABLE + "("
				+ DatabaseAdapter.ID + " STRING,"
				+ DatabaseAdapter.COURSE_NAME1 + " STRING,"
				+ DatabaseAdapter.TEACHER + " STRING,"
				+ DatabaseAdapter.PLACE	+ " STRING,"
				+ DatabaseAdapter.TIME	+ " STRING,"
				+ DatabaseAdapter.REMARK + " STRING);";
		db.execSQL(sql);
	}

	/**
	 * 更新数据库时使用该函数
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + DatabaseAdapter.COURSE_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + DatabaseAdapter.STUDENT_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + DatabaseAdapter.USER_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + DatabaseAdapter.SOFTWARE_THEME);
		db.execSQL("DROP TABLE IF EXISTS " + DatabaseAdapter.LIBRARY_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + DatabaseAdapter.TEACHER_TIME_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + DatabaseAdapter.STUDENT_TIME_TABLE);
		
		onCreate(db);
	}
}
